﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Redmine.Net.Api;
using Redmine.Net.Api.Types;

namespace RedmineWorkerDotNET.App.Classes
{
    public class RedmineClient
    {
        private static string CONST_S_SERVER_URL = RedmineConfiguration.ServerURL;

        private static int CONST_S_ISSUE_NEW_STATUS_ID = Properties.Settings.Default.Issue_NewStatusId;
        private static int CONST_S_ISSUE_IN_PROGRESS_STATUS_ID = Properties.Settings.Default.Issue_InProgressStatusId;
        private static int CONST_S_ISSUE_CLOSE_STATUS_ID= Properties.Settings.Default.Issue_CloseStatusId;


        private static RedmineManager _objManager;
        private static User _objUser;

        private static Issue _objIssue;
        private static DateTime _dtStartTime;

        public static bool Login(string sUserName, string sPassword)
        {
            try
            {
                _objManager = new RedmineManager(CONST_S_SERVER_URL, sUserName, sPassword);

                _objUser = _objManager.GetCurrentUser();

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static RedmineManager Manager
        {
            get { return _objManager; }
        }

        public static User User
        {
            get { return _objUser; }
        }

        public static void ChangeWorkingIssue(Issue objIssue)
        {
            if (_objIssue != null)
            {
                DoCloseWorkingIssue();
            }

            _dtStartTime = DateTime.Now;
            _objIssue = objIssue;

            if (_objIssue.Status.Id == CONST_S_ISSUE_NEW_STATUS_ID)
            {
                if (_objIssue.AssignedTo.Id != _objUser.Id)
                    _objIssue.AssignedTo = new IdentifiableName { Id = _objUser.Id };
                _objIssue.Status = new IdentifiableName { Id = CONST_S_ISSUE_IN_PROGRESS_STATUS_ID };
                _objManager.UpdateObject<Issue>(_objIssue.Id.ToString(), _objIssue);
            }
            else if (_objIssue.AssignedTo.Id != _objUser.Id)
            {
                _objIssue.AssignedTo = new IdentifiableName { Id = _objUser.Id };
                _objManager.UpdateObject<Issue>(_objIssue.Id.ToString(), _objIssue);
            }
        }

        public static void StopWorkingIssue()
        {
            if (_objIssue != null)
            {
                DoCloseWorkingIssue();
            }
        }

        public static void CloseWorkingIssue()
        {
            if (_objIssue != null)
            {
                _objIssue.Status = new IdentifiableName { Id = CONST_S_ISSUE_CLOSE_STATUS_ID };
                _objManager.UpdateObject<Issue>(_objIssue.Id.ToString(), _objIssue);

                DoCloseWorkingIssue();
            }
        }

        public static bool HasWorkingIssue()
        {
            return _objIssue != null;
        }

        public static TimeSpan GetCurrentTimeWorking()
        {
            return DateTime.Now - _dtStartTime;
        }

        private static void DoCloseWorkingIssue()
        {
            TimeSpan objWorkTime = DateTime.Now - _dtStartTime;

            if (objWorkTime.TotalMinutes > 1)
            {
                TimeEntry objTimeEntry = new TimeEntry();
                objTimeEntry.User = new IdentifiableName { Id = _objUser.Id };
                objTimeEntry.Project = _objIssue.Project;
                objTimeEntry.Issue = new IdentifiableName { Id = _objIssue.Id };
                objTimeEntry.Hours = Convert.ToDecimal(objWorkTime.TotalHours);
                objTimeEntry.SpentOn = _dtStartTime;

                objTimeEntry.Comments = RedmineConfiguration.TimeLogDefaultComment;
                objTimeEntry.Activity = new IdentifiableName { Name=RedmineConfiguration.TimeLogDefaultActivity };

                _objManager.CreateObject<TimeEntry>(objTimeEntry);
            }
            _objIssue = null;
        }
    }
}
